<?php

include 'includes.php';


if (isset($_SESSION['user'])) {

    $items = R::getAll('select * from items where bosses_id = :boss', [':boss' => $_GET['boss']]);
    
    $pj = R::getAll('select * from pjs where usuario_id = :id', [':id' => $_SESSION['user']['id']]);

    $pj1 = $pj[0];

    for ($i = 0; $i < count($items); $i++) {

        //      $Heroico = R::getAll('select * from bislist where pj = :pj and item = :item and dificultad = "H"', [':pj' => $_SESSION['user']['id'], ':item' => $items[$i]['id']]);
        $HeroicoB = R::findOne('bislist', 'pj = :pj and item = :item and dificultad = "H"', [':pj' => $pj1['id'], ':item' => $items[$i]['id']]);

        if (isset($_POST[$items[$i]['id'] . "H"])) {
            if (isset($HeroicoB)) {
                if (isset($_POST[$items[$i]['id'] . "HB"])) {
                    $HeroicoB->bis = 1;
                }  else {
                    $HeroicoB->bis = 0;
                }
                R::store($HeroicoB);
            } else {
                $bisH = R::dispense('bislist');
                $bisH->pj = $pj1['id'];
                $bisH->item = $items[$i]['id'];
                $bisH->dificultad = 'H';
                if (isset($_POST[$items[$i]['id'] . "HB"])) {
                    $bisH->bis = 1;
                }  else {
                    $bisH->bis = 0;
                }
                
                R::store($bisH);
            }
        } else {
            if (isset($HeroicoB)) {
                R::trash($HeroicoB);
            }
        }

        $HeroicoWB = R::findOne('bislist', 'pj = :pj and item = :item and dificultad = "HW"', [':pj' => $pj1['id'], ':item' => $items[$i]['id']]);
        if (isset($_POST[$items[$i]['id'] . "HW"])) {
            if (isset($HeroicoWB)) {
                if (isset($_POST[$items[$i]['id'] . "HWB"])) {
                    $HeroicoWB->bis = 1;
                }  else {
                    $HeroicoWB->bis = 0;
                }
                R::store($HeroicoWB);
            } else {
                $bisHW = R::dispense('bislist');
                $bisHW->pj = $pj1['id'];
                $bisHW->item = $items[$i]['id'];
                $bisHW->dificultad = 'HW';
                
                if (isset($_POST[$items[$i]['id'] . "HWB"])) {
                    $bisHW->bis = 1;
                }  else {
                    $bisHW->bis = 0;
                }
                R::store($bisHW);
            }
        } else {
            if (isset($HeroicoWB)) {
                R::trash($HeroicoWB);
            }
        }
    }
}



  error_reporting(E_ALL);
  $host3 = $_SERVER['HTTP_HOST'];
  $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
  $extra = 'index.php';
  header("Location: http://" . $host3 . $uri . "/" . $extra);
  die();

?>
